﻿using System;
using System.Web.UI;
using Ria.Web.Controls;
using Ria.Infrastructure;

namespace Ria.Presentation.Web
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                fcControlOne.InitialData = DemoDataServices.GetControlOneInitialData();
        }

        protected void fc1IncomingObject(object sender, IncomingObjectEventArgs e)
        {
            gtbLog.Text = string.Format("Selected -> name: {0} dob: {1} kids: {2}\n",
                                        e.Data["name"],
                                        DateTime.Parse(e.Data["dob"].ToString()).ToShortDateString(),
                                        e.Data["kids"]) + gtbLog.Text;
        }

        protected void fc2IncomingObject(object sender, IncomingObjectEventArgs e)
        {
            var action = e.Data["action"].ToString();
            if (action.Equals("hello"))
            {
                gtbLog.Text = "Hello from Flex\n" + gtbLog.Text;
            }
            else if (action.Equals("add"))
            {
                gtbLog.Text = "[Control Two] asked to add record to [Control One]\n" + gtbLog.Text;
                fcControlOne.FlexCallObject(DemoDataServices.GetOneRecord(1));
            }
            else if (action.Equals("clear"))
            {
                gtbLog.Text = "[Control Two] asked to clear [Control One]\n" + gtbLog.Text;
                fcControlOne.FlexCallObject(DemoDataServices.GetClear());
            }
        }

        protected void OnAddRecord(object sender, EventArgs e)
        {
            gtbLog.Text = "AJAX button asked to add record to [Control One]\n" + gtbLog.Text;
            fcControlOne.FlexCallObject(DemoDataServices.GetOneRecord(0));
        }

        protected void OnSayHello(object sender, EventArgs e)
        {
            gtbLog.Text = "AJAX button asked to say hello to [Control Two]\n" + gtbLog.Text;
            fcControlTwo.FlexCallObject(DemoDataServices.GetSayHello());
        }

        protected void glbNoStatesClicked(object sender, EventArgs e)
        {
            Response.Redirect("NoStates.aspx");
        }
    }
}
